package org.example.friend_test.controller;

import org.example.friend_test.entity.User;
import org.example.friend_test.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户控制器
 */
@Controller
@RequestMapping("/user")
public class UserController {
    
    @Autowired
    private UserService userService;
    
    /**
     * 显示登录页面
     */
    @GetMapping("/login")
    public String showLoginPage() {
        return "redirect:/login.html";
    }
    
    /**
     * 用户登录
     */
    @PostMapping("/login")
    @ResponseBody
    public Map<String, Object> login(@RequestParam String username, 
                                   @RequestParam String password, 
                                   HttpSession session) {
        Map<String, Object> result = new HashMap<>();
        
        try {
            User user = userService.login(username, password);
            if (user != null) {
                // 登录成功，将用户信息存入session
                session.setAttribute("user", user);
                result.put("success", true);
                result.put("message", "登录成功");
                result.put("redirect", "/friend/list");
            } else {
                result.put("success", false);
                result.put("message", "用户名或密码错误");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "登录失败：" + e.getMessage());
        }
        
        return result;
    }
    
    /**
     * 用户登出
     */
    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/login.html";
    }
    
    /**
     * 显示用户列表页面
     */
    @GetMapping("/list")
    public String showUserList(Model model) {
        model.addAttribute("users", userService.findAllUsers());
        return "redirect:/user-list.html";
    }
    
    /**
     * 显示用户详情页面
     */
    @GetMapping("/detail/{id}")
    public String showUserDetail(@PathVariable Long id, Model model) {
        model.addAttribute("user", userService.findUserById(id));
        return "redirect:/user-detail.html?id=" + id;
    }
    
    /**
     * 显示创建用户页面
     */
    @GetMapping("/create")
    public String showCreateUserPage() {
        return "redirect:/user-create.html";
    }
    
    /**
     * 创建新用户
     */
    @PostMapping("/create")
    @ResponseBody
    public Map<String, Object> createUser(@RequestBody User user) {
        Map<String, Object> result = new HashMap<>();
        
        try {
            User newUser = userService.createUser(user);
            result.put("success", true);
            result.put("message", "用户创建成功");
            result.put("user", newUser);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "用户创建失败：" + e.getMessage());
        }
        
        return result;
    }
    
    /**
     * 显示编辑用户页面
     */
    @GetMapping("/edit/{id}")
    public String showEditUserPage(@PathVariable Long id, Model model) {
        model.addAttribute("user", userService.findUserById(id));
        return "redirect:/user-edit.html?id=" + id;
    }
    
    /**
     * 更新用户信息
     */
    @PostMapping("/update")
    @ResponseBody
    public Map<String, Object> updateUser(@RequestBody User user) {
        Map<String, Object> result = new HashMap<>();
        
        try {
            User updatedUser = userService.updateUser(user);
            result.put("success", true);
            result.put("message", "用户信息更新成功");
            result.put("user", updatedUser);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "用户信息更新失败：" + e.getMessage());
        }
        
        return result;
    }
    
    /**
     * 删除用户
     */
    @PostMapping("/delete/{id}")
    @ResponseBody
    public Map<String, Object> deleteUser(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        
        try {
            boolean success = userService.deleteUser(id);
            if (success) {
                result.put("success", true);
                result.put("message", "用户删除成功");
            } else {
                result.put("success", false);
                result.put("message", "用户删除失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "用户删除失败：" + e.getMessage());
        }
        
        return result;
    }
}
